#ifndef DYNAMICLIST_H
#define DYNAMICLIST_H

#include "SeqList.h"
#include "Exception.h"

#include <iostream>

using namespace std;
namespace DSLib
{

template <typename T>
class DynamicList : public SeqList<T>
{
protected:
    int m_capacity;
public:
    DynamicList(int capacity)
    {
        this->m_array = new T[capacity];
        if (this->m_array)
        {
            m_capacity = capacity;
            this->m_length = 0;
        }
        else
        {
            THROW_EXCEPTION(NoEnoughMemoryException, "alloc memory failed!");
        }
    }

    void resize(int capacity)
    {
        int length = this->m_length < capacity ? this->m_length : capacity;
        T* pNewArray = new T[capacity];
        if (pNewArray)
        {
            for (int i = 0; i < length; i++)
            {
                pNewArray[i] = this->m_array[i];
            }
            T* pTemp = this->m_array;
            this->m_array = pNewArray;
            this->m_length = length;
            delete[] pTemp;
        }
        else
        {
            THROW_EXCEPTION(NoEnoughMemoryException, "alloc memory failed!");
        }
    }

    int capacity() const
    {
        return m_capacity;
    }

    ~DynamicList()
    {
        if (this->m_array)
        {
            delete[] this->m_array;
            this->m_array = NULL;
        }
    }
};
}
#endif // DYNAMICLIST_H
